using UnityEngine;
using System.Collections;

public class RenderBSPPreview : MonoBehaviour {

    public Texture2D roomTexture;
    public Texture2D corridorTexture;
    public Texture2D newSplitTexture;
    public Texture2D oldSplitTexture;
    public Texture2D background;
	public int dungeonX = 256;
	public int dungeonY = 256;

    string splitAmount = "4";

    enum DrawMode
    {
        DrawSplits,
        DrawRooms
    }

    DrawMode drawMode;

    BSPDungeon dungeon;

	void Start () 
    {
        roomTexture = new Texture2D(1, 1, TextureFormat.ARGB32,false);
        Color32 colorData = new Color32(255, 0, 0, 255);
        roomTexture.SetPixel(0, 0, colorData);        
        roomTexture.Apply(false);

        corridorTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        colorData = new Color32(0, 0, 255, 255);
        corridorTexture.SetPixel(0, 0, colorData);
        corridorTexture.Apply(false);

        newSplitTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        colorData = new Color32(0, 255, 0, 255);
        newSplitTexture.SetPixel(0, 0, colorData);
        newSplitTexture.Apply(false);

        oldSplitTexture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        colorData = new Color32(0, 0, 0, 255);
        oldSplitTexture.SetPixel(0, 0, colorData);
        oldSplitTexture.Apply(false);

        background = new Texture2D(1, 1, TextureFormat.ARGB32, false);
        colorData = new Color32(150, 150, 150, 255);
        background.SetPixel(0, 0, colorData);
        background.Apply(false);

        dungeon = new BSPDungeon();
        //dungeon.GenerateTestData();
		dungeon.dungeonWidth = dungeonX;
		dungeon.dungeonHeight = dungeonY;
		dungeon.Reset();
		
        drawMode = DrawMode.DrawRooms;
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void OnGUI()
    {
        if (Event.current.type.Equals(EventType.Repaint))
        {
            //draw background first
            int centerX = (Screen.width - dungeon.treeData.root.cellSize.width) / 2 + 50;
            int centerY = (Screen.height - dungeon.treeData.root.cellSize.height) / 2;
            Rect backgroundTarget = new Rect(centerX, centerY, dungeon.treeData.root.cellSize.width, dungeon.treeData.root.cellSize.height);
            Graphics.DrawTexture(backgroundTarget, background);

            if (drawMode == DrawMode.DrawSplits)
            {
                //draw old splits first
                foreach (BSPRectangle curSplit in dungeon.treeData.oldSplits)
                {
                    Rect target = curSplit.GetUnityRectangle();
                    target.x += centerX;
                    target.y += centerY;
                    Graphics.DrawTexture(target, oldSplitTexture);
                }
                //draw new splits next
                foreach (BSPRectangle curSplit in dungeon.treeData.newSplits)
                {
                    Rect target = curSplit.GetUnityRectangle();
                    target.x += centerX;
                    target.y += centerY;
                    Graphics.DrawTexture(target, newSplitTexture);
                }
            }
            else if (drawMode == DrawMode.DrawRooms)
            {
                foreach (BSPRectangle curRoom in dungeon.treeData.root.childRooms)
                {
                    Rect target = curRoom.GetUnityRectangle();
                    target.x += centerX;
                    target.y += centerY;
                    if (curRoom.rectType == BSPRectangle.RectangleType.Room) Graphics.DrawTexture(target, roomTexture);
                    else Graphics.DrawTexture(target, corridorTexture);
                }
            }
        }//end repaint check

        //draw UI
        if (GUI.Button(new Rect(5, 5, 120, 30), "Split Once"))
        {
            drawMode = DrawMode.DrawSplits;
            dungeon.treeData.SplitNodes(1);
        }
        splitAmount = GUI.TextField(new Rect(130, 45, 30, 20), splitAmount);
        int splitInt = 0;
        try
        {            
            splitInt = int.Parse(splitAmount);
        }
        catch
        {
            splitInt = 1;
        }
        if (GUI.Button(new Rect(5, 40, 120, 30), "Split X"))
        {
            drawMode = DrawMode.DrawSplits;
            dungeon.treeData.SplitNodes(splitInt);
        }
        if (GUI.Button(new Rect(5, 75, 120, 30), "Generate Rooms"))
        {
            drawMode = DrawMode.DrawRooms;
            dungeon.GenerateRooms();
        }
        if (GUI.Button(new Rect(5, 110, 120, 30), "Generate Corridors"))
        {
            if (drawMode == DrawMode.DrawSplits)
            {
                dungeon.GenerateRooms();
                drawMode = DrawMode.DrawRooms;
            }
            if(dungeon.treeData.root.myRoom == null) dungeon.GenerateCorridors();
        }
        if (GUI.Button(new Rect(5, 150, 120, 30), "Generate All"))
        {
			System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            Profiler.BeginSample("GenAll");
			timer.Start();
            dungeon.Reset();
            dungeon.treeData.SplitNodes(splitInt);
            dungeon.GenerateRooms();
            dungeon.GenerateCorridors();
            drawMode = DrawMode.DrawRooms;
			timer.Stop();
            Profiler.EndSample();
			print ("Time Taken: " + timer.Elapsed.ToString());
        }

        GUI.Label(new Rect(5, 190, 180, 30), "Number of Rooms: " + dungeon.treeData.leafNodes.Count.ToString());
        if (GUI.Button(new Rect(5, 250, 120, 30), "Reset")) dungeon.Reset();
		
		GUILayout.BeginArea(new Rect(5,300,200,400));
		string tempX = GUILayout.TextField(dungeonX.ToString());
		string tempY = GUILayout.TextField	(dungeonY.ToString());
		try
		{
			dungeonX = int.Parse(tempX);
			dungeonY = int.Parse(tempY);
		}
		catch
		{
		}
		if(GUILayout.Button("Set Size"))
		{
			dungeon.dungeonWidth = dungeonX;
			dungeon.dungeonHeight = dungeonY;
			dungeon.Reset();
		}
		GUILayout.EndArea();
		                    
        
    }
}
